package org.ishequ.weibo.adapater;

import java.util.List;
import org.ishequ.miduo.R;
import org.ishequ.weibo.utils.http.AsyncImageLoader;
import org.ishequ.weibo.utils.http.AsyncImageLoader.ImageCallback;
import org.ishequ.widget.HighLightTextView;
import weibo4android.Comment;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CommentAdapter extends BaseAdapter {

	static class ViewHolder {
		ImageView asyncImageView;
		TextView tv_name;
		HighLightTextView tv_text;
	}

	private List<Comment> comments;
	private Context context;

	public CommentAdapter(Context context, List<Comment> comments) {
		this.comments = comments;
		this.context = context;
	}
	
	public CommentAdapter(Context context) {
		this.context = context;
	}

	@Override
	public int getCount() {
		return comments.size();
	}

	@Override
	public Comment getItem(int position) {
		return comments.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			LinearLayout view = new LinearLayout(context);
			view.setOrientation(LinearLayout.HORIZONTAL);

			ImageView asyncImageView = new ImageView(context);
			HighLightTextView tv_text = new HighLightTextView(context);
			view.addView(asyncImageView);

			LinearLayout ll = new LinearLayout(context);
			ll.setOrientation(LinearLayout.VERTICAL);

			TextView tv_name = new TextView(context);
			ll.addView(tv_name);
			ll.addView(tv_text);

			view.addView(ll);

			convertView = view;
			holder = new ViewHolder();
			holder.asyncImageView = asyncImageView;
			holder.tv_text = tv_text;
			holder.tv_name = tv_name;
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		// 异步加载头像
		AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
		Drawable cachedImage = asyncImageLoader.loadDrawable(comments.get(
				position).getUser().getProfileImageURL().toString(),
				holder.asyncImageView, new ImageCallback() {
					public void imageLoaded(Drawable imageDrawable,
							ImageView imageView, String imageUrl) {
						imageView.setImageDrawable(imageDrawable);
					}
				});
		if (cachedImage == null) {
			holder.asyncImageView.setImageResource(R.drawable.weibo_avatar);
		} else {
			holder.asyncImageView.setImageDrawable(cachedImage);
		}

		holder.asyncImageView.setPadding(10, 10, 10, 10);

		holder.tv_name.setTextColor(Color.BLUE);
		holder.tv_name
				.setText(comments.get(position).getUser().getScreenName());
		holder.tv_name.setPadding(5, 10, 0, 0);

		holder.tv_text.setPadding(10, 10, 10, 10);
		holder.tv_text.setGravity(Gravity.CENTER_VERTICAL);
		holder.tv_text.setText(comments.get(position).getText());
		holder.tv_text.setTextColor(Color.BLACK);
		return convertView;
	}

	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}

}